<?php
/**
 *
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
 * @encoding UTF-8
 *
 * Copyright (c) 2011 Charly Poly <charly.poly[at]live[dot]fr>
 * Copyright (c) 2011 <Prenom Nom> <email>
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
class Inscription implements Page{
    public function main($args=NULL) {
        if (!Session::getInstance()->sessionCheck()) {
            if (isset($args[0]) && $args[0] == "ActivateAccount") {
                if (isset($args[1]) && !empty($args[1])) $this->activate($args[1]);
                else header("Location: /index.php/Login");
            } else if (!POST('validate')) {
                $S = Template::getSmarty();
                $S->display(Template::getPath("Subscribe"));
            } else {
                $this->validate();
            }
        } else {
            header("Location: /index.php/");
        }
    }

    private function validate() {
        if (POST("pseudo") && POST("combination") && POST("email")) {
            if (!Patterns::login($_POST['pseudo'])) $this->Error(i18n::getInstance()->get("SubscribeLoginPatternError"));
            if (strlen($_POST['combination']) < 9) $this->Error(i18n::getInstance()->get("SubscribePasswordPatternError"));
            if (count(Database::select("Compte", "pseudo", "pseudo=".Storage::getInstance()->fetch()->quote($_POST['pseudo']) ) ) != 0) {
                $this->Error(i18n::getInstance()->get("SubscribeLoginDuplicateError"));
            } else if (!Patterns::email($_POST['email'])) {
                $this->Error(i18n::getInstance()->get("SubscribeEmailPatternError"));
            } else {
                if (($pic = $this->picture()) == false) {
                    $pic = "defaultAvatar.jpg";
                }
                //@todo email verification
                if (Session::create($_POST['pseudo'], $_POST['combination'], $pic, $_POST['email'])) {
                    exit("Votre inscription est maintenant effectu&eacute;e, <a href='/index.php/'>Acceder au site pour vous connecter !</a>");
                }
                else 
                    exit (HTML(i18n::getInstance()->get("SubscribeFatalError")));
            }
        } else {
            $this->Error(i18n::getInstance()->get("SubscribeEmptyError"));
        }
    }

    private function Error($msg) {
        $S = Template::getSmarty();
        $S->assign("Error", $msg);
        $S->display(Template::getPath("Subscribe"));
        die();
    }


    private function picture() {
            $error   = $_FILES['avatar']['error'];

            if ($error == UPLOAD_ERR_OK) {
                $fs = filesize($_FILES['avatar']['tmp_name']);
                if ( $fs <= 0 || $fs > 1000000)   return FALSE;
                unset ($fs);
                $name = sha1($_FILES['avatar']['name'].  mt_rand(0, 100000));
                //$finfo = finfo_open(FILEINFO_MIME);
                $MIME = getimagesize($_FILES['avatar']['tmp_name']);
                $MIME = $MIME["mime"];
                if(preg_match("/(jpeg|gif|jpg|png)/i", $MIME)) {
//                    finfo_close($finfo);
                    move_uploaded_file($_FILES['avatar']['tmp_name'], ROOT.DIRECTORY_SEPARATOR.'avatars'.DIRECTORY_SEPARATOR.$name);

                    $dim = getimagesize(ROOT.DIRECTORY_SEPARATOR.'avatars'.DIRECTORY_SEPARATOR.$name);
                    //Manip
                    $thumb = imagecreatetruecolor(100, 100);
                    if (preg_match("/(jpeg|jpg)/i", $MIME)) $source = imagecreatefromjpeg(ROOT.DIRECTORY_SEPARATOR.'avatars'.DIRECTORY_SEPARATOR.$name);
                    else if (preg_match("/(png)/i", $MIME)) $source = imagecreatefrompng(ROOT.DIRECTORY_SEPARATOR.'avatars'.DIRECTORY_SEPARATOR.$name);
                    else if (preg_match("/(gif)/i", $MIME)) $source = imagecreatefromgif(ROOT.DIRECTORY_SEPARATOR.'avatars'.DIRECTORY_SEPARATOR.$name);
                    else  return FALSE;
                    imagecopyresampled($thumb, $source, 0, 0, 0, 0, 100, 100, $dim[0], $dim[1]);
                    if (preg_match("/(jpg)/i", $MIME)) imagejpeg($thumb, ROOT.DIRECTORY_SEPARATOR.'avatars'.DIRECTORY_SEPARATOR.$name);
                    elseif (preg_match("/(gif)/i", $MIME)) imagegif($thumb, ROOT.DIRECTORY_SEPARATOR.'avatars'.DIRECTORY_SEPARATOR.$name);
                    elseif (preg_match("/(png)/i", $MIME)) imagepng($thumb, ROOT.DIRECTORY_SEPARATOR.'avatars'.DIRECTORY_SEPARATOR.$name);
                    return $name;
                } else {
                    return FALSE;
                }
            } else {
                return FALSE;
            }
    }

}
?>
